package com.ruoyi.web.controller.workJob;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.worker.domain.TermAttendanceGroupV2;
import com.ruoyi.worker.domain.TermAttendanceRulesV2Domain;
import com.ruoyi.worker.service.TermAttendanceGroupV2Service;
import com.ruoyi.worker.service.TermAttendanceRulesV2Service;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/v2/workJobApi/termAttendanceGroup")
public class TermAttendanceGroupV2Controller extends BaseController {

    @Resource
    private TermAttendanceGroupV2Service service;

    @Resource
    private TermAttendanceRulesV2Service rulesV2Service;


    /**
     * @author: 陈宇寰
     * @date: 2021-12-22 18:00:20
     * @params:
     * @return:
     * @description: 新增
     */
    @PostMapping
    public AjaxResult addNewOne(@RequestBody TermAttendanceGroupV2 info) {
        info.setCreateBy(getUsername());
        info.setUpdateBy(getUsername());
        service.add(info);
        return AjaxResult.success("操作成功");
    }

    /**
     * @author: 陈宇寰
     * @date: 2021-12-22 18:00:26
     * @params:
     * @return:
     * @description: 删除【批量】
     */
    @DeleteMapping
    public AjaxResult multiDel(@RequestBody List<Integer> ids) {
        service.multiDel(ids);
        return AjaxResult.success("操作成功");
    }

    /**
     * @author: 陈宇寰
     * @date: 2021-12-22 18:00:52
     * @params:
     * @return:
     * @description: 修改
     */
    @PutMapping
    public AjaxResult update(@RequestBody TermAttendanceGroupV2 info) {
        info.setUpdateBy(getUsername());
        service.update(info);
        return AjaxResult.success("操作成功");
    }

    /**
     * @author: 陈宇寰
     * @date: 2021-12-22 18:01:01
     * @params:
     * @return:
     * @description: 修改状态
     */
    @PutMapping("/change")
    public AjaxResult changeStatus(@RequestBody TermAttendanceGroupV2 info) {
        info.setUpdateBy(getUsername());
        service.changeStatus(info);
        return AjaxResult.success("操作成功");
    }

    /**
     * @author: 陈宇寰
     * @date: 2021-12-22 18:01:07
     * @params:
     * @return:
     * @description: 分页条件查询
     * 要注意，表中有四个特殊的考勤组数据，它们的AttendanceType状态为2，这几个数据是不能被修改、删除、查询的，
     * 它们会用在长期工排班中
     */
    @GetMapping
    public TableDataInfo records(TermAttendanceGroupV2 info) {
        startPage();
        return getDataTable(service.records(info));
    }

    /**
     * @author: 陈宇寰
     * @date: 2021-12-29 14:43:56
     * @params:
     * @return:
     * @description: 获取考勤组可选项
     */
    @GetMapping("/options")
    public AjaxResult options() {
        return AjaxResult.success("ok", service.options());
    }

    /**
     * @author: 陈宇寰
     * @date: 2021-12-22 18:01:14
     * @params:
     * @return:
     * @description: 详情
     */
    @GetMapping("/{id}")
    public AjaxResult detail(@PathVariable Integer id) {
        return AjaxResult.success("查询成功", service.queryOne(id));
    }

    /**
     * @author: 陈宇寰
     * @date: 2021-12-22 18:02:57
     * @params:
     * @return:
     * @description: 新增规则
     */
    @PostMapping("/rules")
    public AjaxResult addOrUpdateRules(@RequestBody List<TermAttendanceRulesV2Domain> param) {
        param.forEach(item -> {
            item.setCreateBy(getUsername());
            item.setUpdateBy(getUsername());
        });
        rulesV2Service.addOrUpdateRecord(param);
        return AjaxResult.success("ok");
    }


    /**
     * @author: 陈宇寰
     * @date: 2021-12-23 09:27:38
     * @params:
     * @return:
     * @description: 查询所有规则记录
     */
    @GetMapping("/rules")
    public AjaxResult rulesRecords() {
        return AjaxResult.success("ok", rulesV2Service.ruleRecords());
    }
}
